package com.qing.module;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.qing.common.base.BaseActivity;
import com.qing.common.executor.ExecutorFactory;
import com.qing.common.ktx.CommonKtxKt;
import com.qing.common.util.AudioWaveformUtils;
import com.qing.common.util.FileUtils;
import com.qing.common.util.LogUtil;
import com.qing.common.view.DeviceButton;
import com.qing.common.view.PhotoPreviewActivity;
import com.qing.module.databinding.ActivityMainBinding;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

public class MainActivity extends BaseActivity<ActivityMainBinding> {
    @Override
    protected ActivityMainBinding createViewBinding() {
        return ActivityMainBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
    }

    @Override
    protected void initListener() {
        getBinding().image1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoPreviewActivity.startActivity(MainActivity.this, new View[]{v, getBinding().image2}, new int[]{com.qing.common.R.drawable.ic_empty, R.drawable.ic_launcher_background});
            }
        });

        getBinding().image2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // PhotoPreviewActivity.startActivity(MainActivity.this, v, R.drawable.ic_launcher_background);
                Intent intent = new Intent(MainActivity.this, MusicActivity.class);
                startActivity(intent);
            }
        });

        getBinding().text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ExecutorFactory.INSTANCE.getOrCreateBackgroundExecutor().post(new Runnable() {
                    @Override
                    public void run() {
                        long blockTime = CommonKtxKt.blockTime(() -> {
                            short[] pcmData = null;
                            File documentDir = getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
                            if (documentDir != null) {
                                pcmData = AudioWaveformUtils.INSTANCE.decodeMp32Pcm(MainActivity.this, documentDir.getAbsolutePath() + File.separator + "music" + File.separator + "test.mp3");
                                float[] waveform = AudioWaveformUtils.INSTANCE.generateWaveform(pcmData, 80000);
                                getBinding().waveform.setWaveform(waveform);
                                LogUtil.INSTANCE.d(TAG, "waveform pcmData: " + pcmData.length);
                                LogUtil.INSTANCE.d(TAG, "waveform pcmData: " + Arrays.toString(pcmData));
                                LogUtil.INSTANCE.d(TAG, "waveform waveform: " + waveform.length);
                                LogUtil.INSTANCE.d(TAG, "waveform waveform: " + Arrays.toString(waveform));
                            }
                            return null;
                        });
                        LogUtil.INSTANCE.d(TAG, "生成成功，耗时: " + blockTime + "ms");
                    }
                });
            }
        });

        getBinding().device.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeType();
            }
        });

        getBinding().tvLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ExecutorFactory.INSTANCE.getOrCreateBackgroundExecutor().post(() -> {
                    // LogUtil.INSTANCE.d(TAG, "开始解压日志");
                    File logFile = FileUtils.INSTANCE.getExternalLogFile(MainActivity.this);
                    LogUtil.INSTANCE.decompressLog(logFile.getAbsolutePath(), logFile.getAbsolutePath().replace(".gz", ".log"));
                    Log.i(TAG, "onClick: 日志解压完成");
                    // LogUtil.INSTANCE.d(TAG, "日志解压完成");
                });
            }
        });
    }

    private void changeType() {
        int type = getBinding().device.getType();
        switch (type) {
            case DeviceButton.TYPE_HEADSET:
                getBinding().device.setType(DeviceButton.TYPE_HEADSET_READING);
                break;
            case DeviceButton.TYPE_HEADSET_READING:
                getBinding().device.setType(DeviceButton.TYPE_PHONE);
                break;
            case DeviceButton.TYPE_PHONE:
                getBinding().device.setType(DeviceButton.TYPE_PHONE_READING);
                break;
            case DeviceButton.TYPE_PHONE_READING:
                getBinding().device.setType(DeviceButton.TYPE_HEADSET);
                break;
        }
    }
}
